package org.soarsky.wealthchengdu.module.load;

import org.soarsky.wealthchengdu.R;
import org.soarsky.wealthchengdu.module.home.HomeActivty;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * 
 * @author 胡伟 启动界面 下午3:35:42
 */
public class StarActivity extends Activity {
	ImageView iv_image_one, iv_image_two, iv_image_three, iv_image_four;
	private Handler handler;
	int i = 1;
	Animation anims;
	Animation animOne, animTwo, animThree, animFour;
	Bitmap oneBack, twoBack, threeBack, fourBack;
	Bitmap oneFace, twoFace, threeFace, fourFace;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_star);
		initView();
		handler = new Handler() {
			public void handleMessage(Message msg) {
				if (msg.what == 1) {
//					Animation anim = AnimationUtils.loadAnimation(
//							StarActivity.this, R.anim.open_one);
//					iv_image_one.startAnimation(anim);
					iv_image_one.startAnimation(animOne);
				}
				if (msg.what == 2) {
//					Animation anim = AnimationUtils.loadAnimation(
//							StarActivity.this, R.anim.open_two);
					iv_image_one.startAnimation(anims);
//					iv_image_two.startAnimation(anim);
					iv_image_two.startAnimation(animTwo);
					iv_image_one.setVisibility(View.GONE);
				}
				if (msg.what == 3) {
					iv_image_two.setVisibility(View.GONE);
//					Animation anim = AnimationUtils.loadAnimation(
//							StarActivity.this, R.anim.open_three);
					iv_image_two.startAnimation(anims);
//					iv_image_three.startAnimation(anim);
					iv_image_three.startAnimation(animThree);
					iv_image_two.setVisibility(View.GONE);
				}
				if (msg.what == 4) {

					//
//					Animation anim = AnimationUtils.loadAnimation(
//							StarActivity.this, R.anim.open_four);
					iv_image_three.startAnimation(anims);
//					iv_image_four.startAnimation(anim);
					iv_image_four.startAnimation(animFour);
					iv_image_three.setVisibility(View.GONE);
					
					
				}
				if (msg.what == 5) {
					handler.removeCallbacks(running);
					i = 1;
					iv_image_one.setBackgroundDrawable(null);
					iv_image_two.setBackgroundDrawable(null);
					iv_image_three.setBackgroundDrawable(null);
					iv_image_four.setBackgroundDrawable(null);
					iv_image_one.setImageBitmap(null);
					iv_image_two.setImageBitmap(null);
					iv_image_three.setImageBitmap(null);
					iv_image_four.setImageBitmap(null);
					oneBack.recycle();
					oneBack = null;
					twoBack.recycle();
					twoBack = null;
					threeBack.recycle();
					threeBack = null;
					fourBack.recycle();
					fourBack = null;
					oneFace.recycle();
					oneFace = null;
					twoFace.recycle();
					twoFace = null;
					threeFace.recycle();
					threeFace = null;
					System.gc();

					Intent intent=new Intent(StarActivity.this,HomeActivty.class);
					startActivity(intent);
					StarActivity.this.finish();
				}
			}
		};
//		new Timer().schedule(new TimerTask() {
//
//			@Override
//			public void run() {
//				// TODO Auto-generated method stub
//				Message msg = new Message();
//				msg.what = i;
//				i++;
//				handler.sendMessage(msg);
//			}
//		}, 0, 1800);
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		handler.post(runnable);
	}
    //初始控件
	public void initView() {
		iv_image_one = (ImageView) findViewById(R.id.iv_image_one);
		iv_image_two = (ImageView) findViewById(R.id.iv_image_two);
		iv_image_three = (ImageView) findViewById(R.id.iv_image_three);
		iv_image_four = (ImageView) findViewById(R.id.iv_image_four);
//		iv_image_one.setBackgroundResource(R.drawable.bg_star_one);
//		iv_image_one.setImageResource(R.drawable.bg_face_1);
//		iv_image_two.setBackgroundResource(R.drawable.bg_star_two);
//		iv_image_two.setImageResource(R.drawable.bg_face_2);
//		iv_image_three.setBackgroundResource(R.drawable.bg_star_three);
//		iv_image_three.setImageResource(R.drawable.bg_face_2);
//		iv_image_four.setBackgroundResource(R.drawable.bg_star_four);
//		iv_image_four.setImageResource(R.drawable.bg_face_1);
//		anims = AnimationUtils.loadAnimation(StarActivity.this, R.anim.close);
	}




	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		handler.removeCallbacks(runnable);
		i = 1;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
	    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { //按下的如果是BACK，同时没有重复
	    	handler.removeCallbacks(runnable);
	    	i = 1;
	    	System.exit(0);
	    	return true;
	    }

	    return super.onKeyDown(keyCode, event);
	}



    
	Runnable runnable = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
//			iv_image_one.setBackgroundResource(R.drawable.bg_star_one);
//			iv_image_one.setImageResource(R.drawable.bg_face_1);
//			iv_image_two.setBackgroundResource(R.drawable.bg_star_two);
//			iv_image_two.setImageResource(R.drawable.bg_face_2);
//			iv_image_three.setBackgroundResource(R.drawable.bg_star_three);
//			iv_image_three.setImageResource(R.drawable.bg_face_2);
//			iv_image_four.setBackgroundResource(R.drawable.bg_star_four);
//			iv_image_four.setImageResource(R.drawable.bg_face_3);
			
			Resources res = StarActivity.this.getResources();
			oneBack = BitmapFactory.decodeResource(res, R.drawable.bg_star_one);
			twoBack = BitmapFactory.decodeResource(res, R.drawable.bg_star_two);
			threeBack = BitmapFactory.decodeResource(res, R.drawable.bg_star_three);
			fourBack = BitmapFactory.decodeResource(res, R.drawable.bg_star_four);
			oneFace = BitmapFactory.decodeResource(res, R.drawable.bg_face_1);
			twoFace = BitmapFactory.decodeResource(res, R.drawable.bg_face_2);
			threeFace = BitmapFactory.decodeResource(res, R.drawable.bg_face_3);
			iv_image_one.setBackgroundDrawable(new BitmapDrawable(oneBack));
			iv_image_one.setImageBitmap(oneFace);
			iv_image_two.setBackgroundDrawable(new BitmapDrawable(twoBack));
			iv_image_two.setImageBitmap(twoFace);
			iv_image_three.setBackgroundDrawable(new BitmapDrawable(threeBack));
			iv_image_three.setImageBitmap(twoFace);
			iv_image_four.setBackgroundDrawable(new BitmapDrawable(fourBack));
			iv_image_four.setImageBitmap(threeFace);
			
			anims = AnimationUtils.loadAnimation(StarActivity.this, R.anim.close);
			animOne = AnimationUtils.loadAnimation(
					StarActivity.this, R.anim.open_one);
			animTwo = AnimationUtils.loadAnimation(
					StarActivity.this, R.anim.open_two);
			animThree = AnimationUtils.loadAnimation(
					StarActivity.this, R.anim.open_three);
			animFour = AnimationUtils.loadAnimation(
					StarActivity.this, R.anim.open_four);
			
			Message msg = new Message();
			msg.what = i;
			i++;
			handler.sendMessage(msg);
			handler.postDelayed(running,1000);
		}
	};
	Runnable running = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			Message msg = new Message();
			msg.what = i;
			i++;
			handler.sendMessage(msg);	
			handler.postDelayed(running,1000);
		}
	};

}
